package com.job.client.posts.Listener;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.job.admin.model.audit.constant.StatusConstant;
import com.job.admin.model.audit.pojo.AuditPosts;
import com.job.client.common.exception.MyselfException;
import com.job.client.common.result.ResponseResult;
import com.job.client.model.posts.constant.PostsStatusConstant;
import com.job.client.model.posts.pojo.Posts;
import com.job.client.model.rabbitmq.AuditResultConstant;
import com.job.client.model.rabbitmq.DraftConstant;
import com.job.client.model.rabbitmq.MessageConstant;
import com.job.client.model.rabbitmq.MysqlToEsGDSNConstant;
import com.job.client.posts.mapper.PostsMapper;
import com.job.feign.admin.audit.AuditFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
@Slf4j
public class PostsListener {
    @Autowired
    private PostsMapper postsMapper;
    @Autowired
    private AuditFeign auditFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RabbitListener(queues = DraftConstant.QUEUE_DRAFT_POSTS)
    public void DraftPostsListener(String postsStr){
        if(postsStr==null){
            log.error("职位草稿箱队列获取的数据为空");
        }
        Posts posts = JSON.parseObject(postsStr, Posts.class);
        if(posts==null){
            log.error("JSON转换出问题");
        }
        posts.setStatus(0);
        int i = postsMapper.insert(posts);
        if(i!=1){
            log.error("未认证的草稿数据插入到数据库失败");
        }
    }

    @RabbitListener(queues = AuditResultConstant.QUEUE_AUDIT_POSTS_RESULT)
    public void setPosts(String dataJSON){
        if(dataJSON == null){
            log.error("职位结果队列接收到数据为空");
            return;
        }
        HashMap<String,String> data = JSON.parseObject(dataJSON, HashMap.class);
        String auditPostsId = data.get("auditPostsId");
        String result = data.get("result");
        ResponseResult responseResult = auditFeign.getPostsResultById(auditPostsId);
        if(responseResult.getCode() != 200){
            log.error("获取职位审核结果数据失败");
            return;
        }
        AuditPosts auditPosts = JSON.parseObject(JSON.toJSONString(responseResult.getData()), AuditPosts.class);
        if(postsMapper == null){
            log.error("JSON数据转换失败");
            return;
        }

        // 整合rabbitmq发送的数据
        HashMap<String,String> message = new HashMap<>();
        message.put("userId",auditPosts.getUserId()); // 用户的id
        message.put("result",result); // 是否审核通过

        if(StatusConstant.STATUS_AUDIT_SUCCESS.equals(Integer.valueOf(result))){ //审核成功
            UpdateWrapper<Posts> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", PostsStatusConstant.TO_BE_RELEASED).eq("posts_id",auditPosts.getPostsId());
            int i = postsMapper.update(updateWrapper);
            if(i != 1){
                log.error("修改职位数据失败");
                return;
            }
            // 发布rabbitmq实现es和mysql数据同步
            Posts posts = postsMapper.selectById(auditPosts.getPostsId());
            rabbitTemplate.convertAndSend(MysqlToEsGDSNConstant.EXCHANGE_GDSN,MysqlToEsGDSNConstant.ROUTINGKEY_POSTS,JSON.toJSONString(posts));
        }else{
            UpdateWrapper<Posts> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", PostsStatusConstant.AUDIT_FAILURE).eq("posts_id",auditPosts.getPostsId());
            int i = postsMapper.update(updateWrapper);
            if(i != 1){
                log.error("修改职位数据失败");
                return;
            }
            message.put("reason",auditPosts.getReason());
        }
        // 发送mq到消息队列
        rabbitTemplate.convertAndSend(
                MessageConstant.EXCHANGE_MESSAGE,
                MessageConstant.ROUTINGKEY_POSTS,
                JSON.toJSONString(message));
    }
}
